package com.example.demo.controller;



import com.example.demo.pojo.Administrator;
import com.example.demo.service.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;

/**
 * @author yanglup
 * @create 2020-10-22 14:48
 */
@Controller
public class BackLoginController {
    @Autowired
    AdministratorService administratorService;
    @RequestMapping("/user/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession session){
        //session里数据其它页面可以使用   model里只能在接下来页面使用
        String myusername;
        String mypassword;
        try {
            Administrator check=administratorService.logincheck(username,password);
            myusername=check.getUsername();
            mypassword=check.getPassword();
        }catch (Exception e){
            myusername="";
            mypassword="";
        }
        if(myusername.equals(username) && mypassword.equals(password)){
//        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
            session.setAttribute("loginUser",username);
            return "redirect:/index.html";//重定向到main.html  如果访问main.html-->到dashboard页面
        }
        else {
//            model.addAttribute("msg","用户名或密码错误");
            session.setAttribute("msg", "用户名或密码错误");
//            return "login";
            return "redirect:/login.html";
        }


    }


    @RequestMapping("/user/logout")
    public String logout(HttpSession session){
        session.invalidate();//注销session
        return "redirect:/login.html";
    }
}
